package top.coolrainly.module.pb.service.user;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import jodd.util.StringUtil;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import top.coolrainly.module.pb.controller.admin.user.vo.*;
import top.coolrainly.module.pb.dal.dataobject.user.MemberUserDO;
import top.coolrainly.framework.common.pojo.PageResult;
import top.coolrainly.framework.common.pojo.PageParam;
import top.coolrainly.framework.common.util.object.BeanUtils;

import top.coolrainly.module.pb.dal.mysql.user.MemberUserMapper;

import static top.coolrainly.framework.common.exception.util.ServiceExceptionUtil.exception;
import static top.coolrainly.module.pb.enums.ErrorCodeConstants.*;

/**
 * 用户信息 Service 实现类
 *
 * @author CoolRainLy
 */
@Service
@Validated
public class MemberUserServiceImpl implements MemberUserService {

    @Resource
    private MemberUserMapper memberUserMapper;
    @Resource
    private PasswordEncoder passwordEncoder;

    @Override
    public Long createMemberUser(MemberUserSaveReqVO createReqVO) {
        validateUsernameExists(createReqVO.getUsername());
        // 插入
        MemberUserDO memberUser = BeanUtils.toBean(createReqVO, MemberUserDO.class);
        memberUser.setPassword(encodePassword(createReqVO.getPassword()));
        memberUserMapper.insert(memberUser);
        // 返回
        return memberUser.getId();
    }

    @Override
    public void updateMemberUser(MemberUserSaveReqVO updateReqVO) {
        // 校验存在
        validateMemberUserExists(updateReqVO.getId());
        // 更新
        MemberUserDO updateObj = BeanUtils.toBean(updateReqVO, MemberUserDO.class);
        if (StringUtil.isNotEmpty(updateReqVO.getPassword())) {
            updateObj.setPassword(encodePassword(updateReqVO.getPassword()));
        }
        memberUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteMemberUser(Long id) {
        // 校验存在
        validateMemberUserExists(id);
        // 删除
        memberUserMapper.deleteById(id);
    }

    private void validateMemberUserExists(Long id) {
        if (memberUserMapper.selectById(id) == null) {
            throw exception(MEMBER_USER_NOT_EXISTS);
        }
    }

    private void validateUsernameExists(String username) {
        if (memberUserMapper.selectByUsername(username) != null) {
            throw exception(USER_NAME_EXISTS);
        }
    }
    public MemberUserDO getMemberUser(Long id) {
        return memberUserMapper.selectById(id);
    }

    @Override
    public PageResult<MemberUserDO> getMemberUserPage(MemberUserPageReqVO pageReqVO) {
        return memberUserMapper.selectPage(pageReqVO);
    }

    @Override
    public MemberUserDO getUserByUsername(String username) {
        return memberUserMapper.selectByUsername(username);
    }

    @Override
    public boolean isPasswordMatch(String rawPassword, String encodedPassword) {
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }

    /**
     * 对密码进行加密
     *
     * @param password 密码
     * @return 加密后的密码
     */
    private String encodePassword(String password) {
        return passwordEncoder.encode(password);
    }

}